﻿Imports System.Net.Mail

Partial Class signup
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim errorMsg As String
        errorMsg = ""
        Dim ctzn As Boolean = Nothing
        'If Boolean.TryParse(citizenList.SelectedValue.ToString, True) Then
        '    ctzn = Boolean.Parse(citizenList.SelectedValue.ToString)
        'End If

        Dim fname As String = firstname.Text
        Dim lname As String = lastname.Text
        Dim email As String = emailbox.Text
        Dim phone As String = phonebox.Text
        Dim studentid As String = studentbox.Text
        Dim uniID As Integer = Integer.Parse(uniList.SelectedValue)
        Dim subject As String = SubjectList.SelectedValue.ToString
        Dim year As String = yearList.SelectedValue.ToString()
        Dim career As String = careerList.SelectedValue.ToString()
        Dim degree As String = DegreeList.SelectedValue.ToString
        Dim gender As String = genderList.SelectedValue.ToString()
        Dim ethnicity As String = ethnicityList.SelectedValue.ToString
        Dim ukcitizen As Boolean = ctzn
        Dim age As String = ageList.SelectedValue.ToString()
        Dim paid As Boolean = CheckBox1.Checked


        Dim exist As [Boolean] = ACSService.IsMemberExist(email, uniID)
        If exist Then
            msgLabel.Text = "This member already exists"
            'Response.Write("<script>alert('This member already exist');</script>")
        Else
            Dim insert As [Boolean] = ACSService.addNewMember(fname, lname, email, phone, studentid, uniID, _
             subject, year, degree, career, gender, ethnicity, _
             ukcitizen, age, paid)

            If insert Then
                Dim str As New StringBuilder()
                str.AppendLine("<p>Hello " + firstname.Text + " " + lastname.Text + ", your registration with ACS was successful</p>")
                str.AppendLine("<p>Congratulations, your registration has been successful. Your ACS will be in contact soon about the next steps.</p>")
                str.AppendLine("<p>In the meantime, why not share the news with your friends.</p>")
                str.AppendLine("<p>Copy and paste this simple message on facebook, twitter and other social networks. ""I just joined my university's African Caribbean Society. You can sign-up through myacs.co.uk""</p>")
                str.AppendLine("<p>Have fun,</p>")
                str.AppendLine("<p>ACS Project</p>")

                Dim body As String = str.ToString()

                Dim message As New MailMessage(New MailAddress("no-reply@acs-membership.com", "ACS Membership"), New MailAddress(email))

                message.Subject = "Welcome to ACS"
                message.Body = body
                message.BodyEncoding = Encoding.ASCII
                message.IsBodyHtml = True

                Dim smtp As New SmtpClient()
                Try
                    smtp.Send(message)
                Catch ex As Exception
                    'mail not sent
                End Try


                msgLabel.Text = "New member added"
                Response.Redirect("thank-you.aspx")
                'Response.Write("<script>alert('New member added');</script>")
            Else
                msgLabel.Text = "Error: member not added"
                'Response.Write("<script>alert('Error: member not added');</script>")
            End If
        End If

        ' Response.Write("<script>alert('Error: member not added');</script>")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Page.MaintainScrollPositionOnPostBack = True

        If (IsPostBack) Then
            ClientScript.RegisterStartupScript(Me.GetType(), "hash", "location.hash = '#third_signup';", True)
        End If

        If Roles.IsUserInRole("Role_Uni_Admin") Then
            Dim uni_id As Integer = ACSService.getAdminUni(Membership.GetUser().ProviderUserKey.ToString)
            UniID.Value = uni_id
            uniList.SelectedValue = uni_id
            uniList.Enabled = False
        ElseIf Roles.IsUserInRole("Role_Super_Admin") Then

        End If



    End Sub
End Class
